﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using MySql.Data.MySqlClient;

namespace HousingDistribution
{
    class HousingInfoMatch
    {
        private string fullAddr = null;
        string streetno27 = "27";
        string streetno38 = "38";
        string streetno48 = "48";

        //获取idhouse, 若没有匹配的数据，return 0
        public int GetHousingIdFromMySql(HousingInfo info)
        {
            fullAddr = info.addr.Trim();   //27号院49楼丙门102室，48号院5楼201室。

            string streetno = GetStreetNo(fullAddr);
            string buildingno = GetBuildingNo(fullAddr);
            string doorway = GetDoorWay(fullAddr);
            string roomno = GetRoomNo(fullAddr);
            string sqladdr = GetSqlStr(streetno, buildingno, doorway, roomno);

            if (sqladdr != null)
            {
                DataSet ds = DBProc.Instance().ReadData(sqladdr);
                DataTable t = ds.Tables[0];

                if (t.Rows.Count > 1)
                {
                    Console.WriteLine("查询房子时一个person现住址对应了多套房子");
                }
                else if (t.Rows.Count == 1)
                {
                    DataRow dr = t.Rows[0];
                    object o = dr[0];
                    int id = (int)o;
                    return id;
                }
            }
                    
            return 0;
        }

        public string GetStreetNo(string addr)
        {
            if (addr.IndexOf("号院") > 0)
            {
            string streetno = addr.Substring(0,addr.IndexOf("号院"));
            return streetno;
            }
            else
                return null;
        }

        private string GetBuildingNo(string addr)
        {
            if ((addr.IndexOf("号院") > 0) && (addr.IndexOf("楼") > 0))
            {
                string buildingno = addr.Substring(addr.IndexOf("号院") + 2, addr.IndexOf("楼") - addr.IndexOf("号院") - 2);
                return buildingno;
            }
            return null;
        }

        private string GetDoorWay(string addr)
        {
            if ((addr.IndexOf("号院") > 0) && (addr.IndexOf("楼") > 0) && (addr.IndexOf("门") > 0))
            {
                string doorway = addr.Substring(addr.IndexOf("楼")+1 , addr.IndexOf("门")-addr.IndexOf("楼")-1);
                return doorway;
            }
            else
            return null;
        }

        private string GetFloor(string addr)
        {
            return null;
        }

        private string GetRoomNo(string addr)
        {
            if ((addr.IndexOf("号院") > 0) && (addr.IndexOf("楼") > 0) && (addr.IndexOf("门") > 0) && addr.IndexOf("室")>0)
            {
                string roomno = addr.Substring(addr.IndexOf("门") + 1, addr.IndexOf("室") - addr.IndexOf("门") - 1);
                return roomno;
            }
            else if ((addr.IndexOf("号院") > 0) && (addr.IndexOf("楼") > 0) && addr.IndexOf("室") > 0)
            {
                string roomno = addr.Substring(addr.IndexOf("楼") + 1, addr.IndexOf("室") - addr.IndexOf("楼") - 1);
                return roomno;
            }
            else
                return null;
        }

        private string GetSqlStr(string streetno, string buildingno, string doorway, string roomno)
        {
            string sqladdr = null;

            if ((streetno == streetno27) || (streetno == streetno38) || (streetno == streetno48))
            { 
                //48号院4,5楼直接有房间号就能对应上。
                if ((streetno == streetno48) && ((buildingno == "4") || (buildingno == "5")))
                {
                    sqladdr = "select * from housing where streetNo='"
                        + streetno + "' and buildingNo='" + buildingno +"' and roomNo='" + roomno + "'";

                }
                 //其他住址需要单元号才能对应上。
                else
                {
                    sqladdr = "select * from housing where streetNo='"
                        + streetno + "' and buildingNo='" + buildingno
                        + "' and doorway='" + doorway +"' and roomNo='" + roomno + "'";
                }
            }

            return sqladdr;
        }
    }
}
